package com.example.product.service.impl;


import com.example.model.product.dto.CategoriesGoodVO;
import com.example.model.product.dto.GoodsDetailVO;
import com.example.model.product.pojo.BnnerImage;
import com.example.model.product.pojo.Goods;
import com.example.product.mapper.GoodsMapper;
import com.example.product.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class GoodsServiceimpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    @Transactional
    public List<CategoriesGoodVO> selectAllGoods() {
        //先查找分类表返回分类id
        List<CategoriesGoodVO> lsitcatrgories = goodsMapper.selectCatrgories();
        for (int i = 0; i < lsitcatrgories.size(); i++) {
            String id = lsitcatrgories.get(i).getId();
            //去goods中找
            List<Goods> listgoods=goodsMapper.selectGoods(id);
            lsitcatrgories.get(i).setGoods(listgoods);
        }
        return lsitcatrgories;
    }

    @Override
    public List<Goods> selectDiffrentCategoriesGoods(String id) {
        List<Goods>  list= goodsMapper.selectDiffrentCategoriesGoods(id);
        return list;
    }

    @Override
    public List<BnnerImage> selectBnnerimage() {
        List<BnnerImage>  list = goodsMapper.selectBnnerimage();
        return list;
    }

    @Override
    public List<Goods> selectNewsGoods() {
        List<Goods> list =  goodsMapper.selectNewsGoods();
        return list;
    }

    @Override
    public List<Goods> selectHotGoods() {
        List<Goods> list =goodsMapper.selectHotGoods();
        return list;
    }


    @Override
    public GoodsDetailVO selectGoodDetail(String id) {
        GoodsDetailVO goods = goodsMapper.selectGoodsDetail(id);
        double price = Double.parseDouble(goods.getPrice());  //
         if (goods.getDiscount()!=null) {
             String discountStr = goods.getDiscount();
             double discount = Double.parseDouble(discountStr);
             double v =  price/(1-discount);  // 计算
             goods.setOldPrice(v);
         }else {
             goods.setOldPrice(price);
         }
        return goods;
    }
}
